package com.golden.order02.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.order02.dao.OrderHisItemMapper;
import com.golden.order02.domain.OrderHisItemDO;
import com.golden.order02.domain.ResultDO;
import com.golden.order02.entity.OrderHisItem;
import com.golden.order02.query.OrderHisItemExample;

@Service
public class OrderHisItemServiceImpl implements OrderHisItemService{
	
	@Autowired
	OrderHisItemMapper orderHisItemMapper;
	
	public ResultDO<OrderHisItemDO> get(Integer id){
		ResultDO<OrderHisItemDO> result=new ResultDO<OrderHisItemDO>();
		BaseServiceImpl<OrderHisItemDO,OrderHisItem> bi=new BaseServiceImpl<>();
		OrderHisItemDO oo=new OrderHisItemDO();
		
		try {
			OrderHisItem selectByPrimaryKey = orderHisItemMapper.selectByPrimaryKey(id);
			OrderHisItemDO t = bi.getT(selectByPrimaryKey, oo);
			result.setData(t);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<List<OrderHisItemDO>> query( OrderHisItemExample example){
		ResultDO<List<OrderHisItemDO>> result=new ResultDO<List<OrderHisItemDO>>();
		BaseServiceImpl<OrderHisItemDO,OrderHisItem> bi=new BaseServiceImpl<>();
		OrderHisItemDO oo=new OrderHisItemDO();
		try {
			List<OrderHisItem> selectByExample = orderHisItemMapper.selectByExample(example);
			System.out.println(selectByExample+"---------"+selectByExample.size());
			List<OrderHisItemDO> tList = bi.getTList(selectByExample, oo);
			if(tList==null) {
				tList=new ArrayList<>();
			}
			result.setData(tList);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<OrderHisItemDO> add( OrderHisItemDO beanDO){
		ResultDO<OrderHisItemDO> result=new ResultDO<OrderHisItemDO>();
		BaseServiceImpl<OrderHisItem, OrderHisItemDO> bi=new BaseServiceImpl<>();
		OrderHisItem oi=new OrderHisItem();
		OrderHisItem t = bi.getT(beanDO, oi);
		try {
			int insert = orderHisItemMapper.insert(t);
			if(insert<=0) {
				throw new Exception();
			}
			beanDO.setId(t.getId());
			result.setData(beanDO);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<Integer> delete( Integer id){
		ResultDO<Integer> result=new ResultDO<Integer>();
		try {
			int deleteByPrimaryKey = orderHisItemMapper.deleteByPrimaryKey(id);
			
			if(deleteByPrimaryKey<=0) {
				throw new Exception();
			}
			result.setData(id);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_DELETE_ERROR);
			result.setErrorMsg(ResultDO.ORDER_DELETE_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<OrderHisItemDO> modify( OrderHisItemDO beanDO){
		ResultDO<OrderHisItemDO> result=new ResultDO<OrderHisItemDO>();
		BaseServiceImpl<OrderHisItem, OrderHisItemDO> bi=new BaseServiceImpl<>();
		OrderHisItem oi=new OrderHisItem();
		OrderHisItem t = bi.getT(beanDO, oi);
		try {
			int insert = orderHisItemMapper.updateByPrimaryKeySelective(t);
			if(insert<=0) {
				throw new Exception();
			}
			result.setData(beanDO);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR);
		}
		return result;
	}

}
